package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user == null){
            return null;
        }
        return user;
    }

    @Override
    public Result updatepassword(String currentPassword, String newPassword, String username) {
        try {
            //根据用户名和密码查询数据库
            User user = userDao.findByUser(username);
            if (user==null){
                return new Result(false,"当前用户名存在正确");
            }
            //获取密码
            String password = user.getPassword();
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            //判断密码是否正确
            boolean flag = encoder.matches(currentPassword, password);
            if (!flag){
                return new Result(false,"当前密码输入不正确");
            }
            //修改密码
            userDao.updatePasswordByUsername(username,newPassword);
            return new Result(true,"密码修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"密码修改失败");
        }
    }

    @Override
    public Result updateByName(String checkName, String encode) {
        try {
            userDao.updatePasswordByUsernameOrTelephone(checkName,encode);
            return new Result(true,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改失败");
        }
    }

    @Override
    public PageResult selectAllByName(QueryPageBean queryPageBean) {
        try {
            //获取查询参数
            String queryString = queryPageBean.getQueryString();
            //分页
            PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
            Page<User> users = userDao.selectAllByName(queryString);
            return new PageResult(users.getTotal(),users.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Result add(Integer[] roleIds, User user) {
        try {
            //添加用户
            userDao.add(user);
            //遍历角色id
            for (Integer roleId : roleIds) {
                //添加中间表数据
                userDao.addUserAndRole(user.getId(),roleId);
            }
            return new Result(true,"添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加用户失败");
        }
    }

    @Override
    public Result update(User user, Integer[] roleIds) {
        try {
            //编辑用户
            userDao.update(user);
            //编辑中间表
            //先删除 在添加
            userDao.deleteRU(user.getId());
            for (Integer roleId : roleIds) {
                userDao.addUserAndRole(user.getId(),roleId);
            }
            return new Result(true,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }

    @Override
    public Result delete(int id) {
        try {
            //查询中间表是否有该用户id
            int count = userDao.findById(id);
            if (count>0){
                //删除中间表再删除用户表
                userDao.deleteRU(id);
            }
            userDao.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
}
